package com.wlb365.financing.modules.sys.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wlb365.financing.modules.sys.entity.Dict;
import com.wlb365.financing.modules.sys.mapper.DictMapper;
import com.wlb365.financing.modules.sys.service.IDictService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 概述：
 * </p>
 *
 * @author zwh
 * @version 1.0
 * @copyRight 北京微利宝信息技术有限公司  All right reserved.
 * @date 2020/3/21
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements IDictService {

    @Override
    public Page<Dict> listDictPage(Dict dict) {
        Page<Dict> page = new Page<>(dict.getCurrent(), dict.getSize());
        if (StrUtil.isBlank(dict.getName())) {
            dict.setParentId(dict.getId() == null ? 0 : dict.getId()); // treegrid默认传的id作为parentId
        }
        List<Dict> dicts = baseMapper.selectDictList(dict, page);
        return page.setRecords(dicts);
    }

    @Override
    public Dict getByCode(String code) {
        return super.getOne(new LambdaQueryWrapper<Dict>().eq(Dict::getCode, code));
    }

    @Override
    public List<Dict> listAllDictItemByCode(String code, Integer type) {
        List<Dict> dicts = baseMapper.selectAllDictItemByCode(code);
        if (type != null) {
            return dicts.stream().filter(s-> type.equals(s.getType())).collect(Collectors.toList());
        }
        return dicts;
    }

    @Override
    public List<Dict> listAllDictItemByCode(String code) {
        return baseMapper.selectAllDictItemByCode(code);
    }


}
